/*
 * ModificationRecord is a helper class for the ease of storing and retrieving unsync
 * actions. It basically acts just like a protocol to unify the storing and retrieving 
 * such information, specifying in what form these commands will be stored and retrieved.
 */
/** @author A0105514U */
// @author A0105514U
package storage;

import global.Constant;

public class ModificationRecord {
	private String modType;
	
	// If it is an add command, addedFileName should be the name of the file added
	// Otherwise, addedFilename would be empty string
	private String addedFileName;
	
	// If it is a delete command, deletedTaskId should be the id of the task on Calendar
	// If the task has not been sync yet(which should not happen), deletedTaskId would be empty string
	// If it is not a delete command, deletedTaskId would also be empty string
	private String deletedTaskId;
	
	public ModificationRecord(String type,String fileName,String taskId){
		modType = type;
		assert(isAddRecord() || isDeleteRecord());
		if(fileName == null){
			fileName = Constant.EMPTY_STRING;
		}
		
		if(taskId == null){
			taskId = Constant.EMPTY_STRING;
		}
		
		addedFileName = fileName;
		deletedTaskId = taskId;
		checkAndRepairRecordIntegrity();
	}
	
	// Check and maintain the integrity of the record
	private void checkAndRepairRecordIntegrity(){
		if(!isAddRecord()){
			addedFileName = Constant.EMPTY_STRING;
		}else{
			assert(addedFileName.length() != 0);
		}
		
		if(!isDeleteRecord()){
			deletedTaskId = Constant.EMPTY_STRING;
		}else{
			assert(deletedTaskId.length() != 0);
		}
	}
	
	public String getModType(){
		return modType;
	}
	
	public String getAddedFileName(){
		if(!isAddRecord()){
			return Constant.EMPTY_STRING;
		}
		
		return addedFileName;
	}
	
	public String getDeletedTaskId(){
		if(!isDeleteRecord()){
			return Constant.EMPTY_STRING;
		}
		return deletedTaskId;
	}
	
	public String toString(){
		return modType+Constant.HEX+addedFileName+Constant.HEX+deletedTaskId;
	}
	
	private boolean isAddRecord(){
		assert(modType != null);
		return modType.equalsIgnoreCase(Constant.ADD_COMMAND_TYPE);
	}
	
	private boolean isDeleteRecord(){
		assert(modType != null);
		return modType.equalsIgnoreCase(Constant.DELETE_COMMAND_TYPE);
	}
}
